# $Revision: 1.0 $
# $Date: 2007/10/09 18:22:36 $
# Bao Nguyen <bn@ucsd.edu>
#
# DESCRIPTION: gpconfig
# LICENSE: GPL

package gpconfig;
use strict;
use warnings;

BEGIN {
   use Exporter ();
   our ( @ISA, @EXPORT_OK );
   @ISA		= qw( Exporter );
   @EXPORT_OK 	= qw( $RETRY $PROBE_LOCK $PROBE_DIR $PROBE_LOG_DIR $EMAIL &tdata_errors &output_errors &set_srb_env &get_domains &run_timed &run_timed_debug );
}
our @EXPORT_OK;

# CONFIGURATION VARIABLES
# ====================================
our $RETRY = 3;

# this variable needs to be read from conf/probe.conf, or not?
our $PROBE_DIR = "/export/home/gridstat/gridprobe";

our $PROBE_LOG_DIR = "$PROBE_DIR/logs";
our $PROBE_LOCK = "$PROBE_DIR/.probe.lock";

# email TO
our $EMAIL = 'noc@nbirn.net';
# ====================================


# Major configuration object - METHDOS
our @METHODS = (
    'SputParallel',
    'SputBulk',
    'SgetParallel',
    'SgetBulk',
    'SrmParallel',
    'SrmBulk',
    'SrbSum'
);

# stream errors from TIMEOUTS
sub tdata_errors {
    my $local = shift;
    my $remote = shift;
    my $error = shift;
    my %methods = @_;
    my $string = '';

    foreach my $method ( @METHODS ) {
        next if $methods{$method};
        $string .= "d $local $remote $method ERROR $error\n";
    }
    return $string;
}

# output errors from TIMEOUTS
sub output_errors {
    my $local = shift;
    my $remote = shift;
    my $error = shift;
    my %methods = @_;

    foreach my $method ( @METHODS ) {
        next if $methods{$method};
        print "d $local $remote $method ERROR $error\n";
    }
}

# Set the ENV vars for SRB
sub set_srb_env {
   $ENV{'srbPort'} = '5825';
   $ENV{'srbUser'} = 'filetest';
   $ENV{'srbAuth'} = '5test';
   $ENV{'srbHost'} = 'bcc-gpop.nbirn.net';
   $ENV{'mdasDomainName'}  ='ucsd-bcc';
   $ENV{'mdasCollectionName'} = '/home/filetest.ucsd-bcc';
   $ENV{'defaultResource'} = "bcc-nas";
}


# get the SRB resource domains
sub get_domains {
	if( !$ENV{'srbUser'} ) {
		set_srb_env();
	}

	open( COM, "/usr/local/bin/SgetR -T logical |" )
		or die "Unable to SgetR -T logical: $!\n";

	my %domains;
	while( <COM> ) {

		# example:
		# rsrc_name: wustl-cnl-nas
		my ( $k, $v ) = split( /:/, $_, 2 );
		next unless $k eq 'rsrc_name';

		# trick to set both vars
		my ( $domain, $fqdn ) = ( $v, '' );
		( $domain ) = $domain =~ m/^\s*(.*?)\s*$/;

		next if $domain =~ /test/;
		$domain =~ s/-nas//;
		next if $domains{$domain};

		# DEBUG _ HOSTS NOT READY
		#next if $domain eq 'nih-cit';
		next if $domain eq 'ohio-bmi';
		#next if $domain eq 'ucsd-fmri';
		#next if $domain eq 'wustl-cnl';
		#next if $domain eq 'eu-mrchgu';
		#next if $domain eq 'mu-esnw';

		# hack to prevent 'bcc' and 'ucsd-bcc' dupes
		next if $domain eq 'bcc';

		# grab the next line and parse
		# netprefix: wustl-cnl-gpop.nbirn.net:NULL:NULL
		(undef, $fqdn, undef ) = split( /:/, <COM> );
		( $fqdn ) = $fqdn =~ m/^\s*(.*?)\s*$/;
		$domains{$domain}{'fqdn'} = $fqdn;	
	}
	close( COM );

	return %domains;
}

# run_timed function
sub run_timed {
	my ( $timeout, $command ) = @_;

	my $script = qq!expect << EOT | dos2unix
	set timeout $timeout
	spawn -noecho $command
	expect {
		timeout { puts "TIMEOUT"; }
	}
EOT
!;

	# actually run the expect code
	my $output = qx( $script );

    return $output;
}

# run_timed function
sub run_timed_debug {
	my ( $timeout, $command ) = @_;

	my $script = qq!expect << EOT | dos2unix
	set timeout $timeout
	spawn -noecho $command
	expect {
		timeout { puts "DEBUG TIMEOUT: $command"; }
	}
EOT
!;

	# actually run the expect code
    print "i about to run timed comand\n";
	my $output = qx( $script );

    print "i just finished timed comand\n";
    return $output;
}

1;
